package com.lgx.mall.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.business.entity.ConsumerInfo;
import com.lgx.mall.business.service.IConsumerInfoService;
import com.lgx.right.entity.Organization;
import com.lgx.right.entity.User;
import com.lgx.right.service.IOrganizationService;
import com.lgx.right.service.IUserService;

@Controller
@RequestMapping("/consumerInfo")
public class ConsumerInfoController {

	@Autowired
	private IConsumerInfoService consumerInfoService;

	public IConsumerInfoService getConsumerInfoService() {
		return consumerInfoService;
	}

	public void setConsumerInfoService(IConsumerInfoService consumerInfoService) {
		this.consumerInfoService = consumerInfoService;
	}
	
	@Autowired
	private IOrganizationService organizationService;
	
	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}
	
	@Autowired
	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@RequestMapping("/consumerInfoMgr.do")
	public String ConsumerInfoMgr(HttpServletRequest request,HttpServletResponse response)
	{
		//消费类型
		List<Dictionary> consumerInfoType = DictUtils.getDictListByKind(DictUtils.CONSUMERINFO_TYPE);
		request.setAttribute("consumerInfoType", consumerInfoType);
		
		return "mall/business/consumerInfo/consumerInfoMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		Object[] params = new Object[10];
		
		String consumerTimeStart = request.getParameter("consumerTimeStart");
		String consumerTimeEnd = request.getParameter("consumerTimeEnd");
		String userMobile = request.getParameter("userMobile");
		String userCardId = request.getParameter("userCardId");
		String consumerInfoType = request.getParameter("consumerInfoType");
		String orgName = request.getParameter("orgName");
		String amountBegin = request.getParameter("amountBegin");
		String amountEnd = request.getParameter("amountEnd");
		
		if(!StringUtils.isEmpty(consumerTimeStart)){
			params[0] = consumerTimeStart+" 00:00:00";
		}
		if(!StringUtils.isEmpty(consumerTimeEnd)){
			params[1] = consumerTimeEnd+" 23:59:59";
		}
		if(!StringUtils.isEmpty(userMobile)){
			params[2] = userMobile;
		}else{
			params[2] = "";
		}
		if(!StringUtils.isEmpty(userCardId)){
			params[3] = userCardId;
		}else{
			params[3] = "";
		}
		if(!StringUtils.isEmpty(consumerInfoType)){
			params[4] = consumerInfoType;
		}
		if(!StringUtils.isEmpty(orgName)){
			params[5] = orgName;
		}else{
			params[5] = "";
		}
		if(!StringUtils.isEmpty(amountBegin)){
			params[6] = amountBegin;
		}
		if(!StringUtils.isEmpty(amountEnd)){
			params[7] = amountEnd;
		}
		
		Pagination page = this.consumerInfoService.findConsumerInfoPagination(params, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		Json2Utils.parseJSON(page, response);
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response)
	{
		String type = request.getParameter("type");
		User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);//得到当前登录人信息
				
		request.setAttribute("type", type);
		
		Organization org = this.organizationService.findOrganizationById(onlineUser.getOrgId());
		request.setAttribute("organization", org);
		request.setAttribute("sscore", org != null?org.getSscore():"0");
		
		return "mall/business/consumerInfo/consumerInfo-init";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response)
	{
		String userCardIdOrMobile = request.getParameter("userCardIdOrMobile");
		if(!StringUtils.isEmpty(userCardIdOrMobile))userCardIdOrMobile = EscapeUtils.unescape(userCardIdOrMobile);
		String amount = request.getParameter("amount");
		if(!StringUtils.isEmpty(amount))amount = EscapeUtils.unescape(amount);
		String score = request.getParameter("score");
		if(!StringUtils.isEmpty(score))score = EscapeUtils.unescape(score);
		
//		if(this.consumerInfoService.findUserByCardIdOrMobile(userCardIdOrMobile).size() == 0){
//			return "0";
//		}
		User user = this.consumerInfoService.findUserByCardIdOrMobile(userCardIdOrMobile).get(0);//根据卡号或手机得到用户信息
		
		User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);//得到当前登录人信息
		Organization org = this.organizationService.findOrganizationById(onlineUser.getOrgId());//根据当前登录人获取对应商家信息
		
		ConsumerInfo c = new ConsumerInfo();
		c.setUserCardId(user.getId()+"");
		c.setTrueName(user.getTruename());
		c.setUserMobile(user.getMobile());
		c.setUserCardId(user.getCard().getId());
		c.setOrgId(org.getId());
		c.setOrgName(org.getName());
		c.setOrgType(org.getType());
		c.setAmount(Double.parseDouble(amount));
		c.setScore(Double.parseDouble(score));
		if(user.getOrgId() == org.getId()){
			c.setType("1");
		}else{
			c.setType("2");
		}
		c.setConsumerTime(new Date());
		
		//给消费的会员添加积分和总积分
		user.getCard().setScore(user.getCard().getScore() + Double.parseDouble(score));
		user.getCard().setTotalScore(user.getCard().getTotalScore() + Double.parseDouble(score));
		//消费商家扣除积分
		org.setSscore(org.getSscore() - Double.parseDouble(score));
		//如果是外卡，给开卡商家返利
		Organization organization = this.organizationService.findOrganizationById(user.getOrgId());
		if(user.getOrgId() != org.getId()){
			if(organization.getType().equals("1")){
				organization.setSscore(organization.getSscore() + Double.parseDouble(score)*Constants.MCREBATE_ORGOUTCARD_TYPE);
			}
			else if(organization.getType().equals("2")){
				organization.setSscore(organization.getSscore() + Double.parseDouble(score)*Constants.MCREBATE_PROMOTIONSTATION_TYPE);
			}
		}
		
		try {
			this.consumerInfoService.createConsumerInfo(c);
			this.organizationService.updateOrganization(org);
			this.organizationService.updateOrganization(organization);
			this.userService.updateUser(user);
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/checkUserCardIdOrMobileExists.do")
	@ResponseBody
	public String checkUserCardIdOrMobileExists(HttpServletRequest request,HttpServletResponse response){
		String userCardIdOrMobile = request.getParameter("userCardIdOrMobile");
		if(this.consumerInfoService.findUserByCardIdOrMobile(userCardIdOrMobile).size() > 0){
			return "0";
		}
		return "1";
	}
}
